//
//  SleepRecentSevenDaysView.swift
//  MoyoungHome
//
//  Created by 李然 on 2022/9/28.
//

import UIKit
import SwiftDate
import MYModule

import RxCocoa
import RxSwift


class SleepRecentSevenDaysCell: SleepBaseCell {
    
    var viewModel: SleepSevenDaysTrendsViewModel?

    let chartHeight:CGFloat = 200

    override func makeUI() {
        super.makeUI()
        
        self.backgroundColor = UIColor.background
        chartView.setMarkerView(markerView)
        
        let dataStack = StackView()
        dataStack.axis = .horizontal
        dataStack.alignment = .firstBaseline
        dataStack.contentMode = .center
        dataStack.addArrangedSubview(hourLabel)
        dataStack.addArrangedSubview(hourUnitLabel)
        dataStack.addArrangedSubview(minuteLabel)
        dataStack.addArrangedSubview(minuteUnitLabel)
        
        let labelStackView = StackView()
        labelStackView.axis = .vertical
        labelStackView.alignment = .center
        
        labelStackView.addArrangedSubview(dataStack)
        labelStackView.addArrangedSubview(timeLabel)
        
        bgView.addSubview(titleLabel)
        bgView.addSubview(chartView)
        bgView.addSubview(labelStackView)
        
       
        labelStackView.snp.makeConstraints { make in
            make.top.equalTo(titleLabel.snp.bottom).offset(20)
            make.centerX.equalToSuperview()
        }
        
        
        titleLabel.snp.makeConstraints { make in
            make.leading.equalTo(26)
            make.top.equalTo(20)
        }
        
        chartView.snp.makeConstraints { make in
            make.top.equalTo(labelStackView.snp.bottom).offset(20)
            make.height.equalTo(chartHeight)
            make.leading.equalTo(6)
            make.trailing.equalTo(-6)
            make.bottom.equalToSuperview().offset(-20)
        }
    }
    
    lazy var titleLabel: Label = {
        let label = Label()
        label.textColor = UIColor.assist_000000
        label.font = Fonts.systemFont(ofSize: 16)
        label.text = "sleep_data_in_the_7_days".language
        return label
    }()
    
    lazy var hourLabel: Label = {
        let label = Label.init()
        label.text = "--"
        label.font = Fonts.boldSystemFont(ofSize: 40)
        label.textColor = UIColor.assist_222222
        return label
    }()
    
    lazy var hourUnitLabel: Label = {
        let label = Label.init()
        label.font = Fonts.systemFont(ofSize: 11)
        label.textColor = UIColor.assist_222222
        label.text = "sleep_hour".language
        return label
    }()
    
    lazy var minuteLabel: Label = {
        let label = Label.init()
        label.text = "--"
        label.font = Fonts.boldSystemFont(ofSize: 40)
        label.textColor = UIColor.assist_222222
        return label
    }()
    
    lazy var minuteUnitLabel: Label = {
        let label = Label.init()
        label.font = Fonts.systemFont(ofSize: 11)
        label.textColor = UIColor.assist_222222
        label.text = "sleep_min".language
        return label
    }()
    
    lazy var timeLabel: Label = {
        let label = Label.init()
        label.text = "sleep_avg_sleep_time".language
        label.font = Fonts.systemFont(ofSize: 11)
        label.textColor = UIColor.assist_222222
        return label
    }()
    
    
  public lazy var chartView: DFBarChartView = {
        let chartView = DFBarChartView()
        
        chartView.setConfig(DFBarChartViewConfig(xAxisLabelTextColor: UIColor.assist_A9A9A9,
                                                 xAxisLineColor: UIColor.Sleep.restful))
        return chartView
    }()
    
    private lazy var markerView: DataTimeShowMarkerView = {
        let markerView = DataTimeShowMarkerView.init(frame: CGRect(x: 0, y: 0, width: 110, height: 40))
        markerView.maxValue = 24
        markerView.textFont = Fonts.systemFont(ofSize: 10)
        markerView.backgroundColor = UIColor.background
        markerView.textBgColor = UIColor.Sleep.restful
        markerView.textColor = .white
            
        return markerView
    }()

    
    override func bind(to cellType: SleepViewModel.CellType) {
        super.bind(to: cellType)
        
        guard let viewModel = cellType.viewModel as? SleepSevenDaysTrendsViewModel else { return }
        
        self.viewModel = viewModel
        

        let disposeBag = DisposeBag()
        
        self.chartView.setYMax(value: SleepSevenDaysTrendsViewModel.YMax)
        
        viewModel.recentSevenDay.subscribe(onNext:{ [weak self] records in
            
            guard let date = viewModel.providerRelay.value?.date else {
                return
            }
            self?.chartView.setXAxis(valueFormatter: RecentSevenDayAxisValueFormatter(date: date))
            self?.chartView.highlightPerTapEnabled = false
            
            self?.chartView.setBarDataModels(records)
            
        }).disposed(by: disposeBag)
        
        
//        Observable.combineLatest(chartView.selectIndex, viewModel.recentSevenSleepTime).map { i, sevenSleepTime -> (hour: String, minute: String) in
//             
//            return sevenSleepTime[i]
//            
//        }.subscribe(onNext: {[weak self] (hour: String, minute: String) in
//            
//            self?.hourLabel.text = hour
//            self?.minuteLabel.text = minute
//            
//        }).disposed(by: disposeBag)
        
        viewModel.avgSleepTime.subscribe(onNext: {[weak self] (hour: String, minute: String) in
            
            self?.hourLabel.text = hour
            self?.minuteLabel.text = minute
            
        }).disposed(by: rx.disposeBag)
        
       
        
        
    }
    
}
